/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年2月6日 下午8:32:02
 */
package com.lenovo.lmrp.server.itsmgateway.test.integration;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import com.lenovo.lmrp.api.itsmgateway.IItsmEngineer;
import com.lenovo.lmrp.api.model.view.ChangedTicketVO;
import com.lenovo.lmrp.api.model.view.LoginData;
import com.lenovo.lmrp.api.model.view.TicketVO;
import com.lenovo.sdc.framework.net.HttpRequest;
import com.lenovo.sdc.framework.net.HttpResponse;
import com.lenovo.sdc.framework.net.HttpSession;
import com.lenovo.sdc.framework.rest.RestClientProxy;
import com.lenovo.sdc.framework.util.JSONHelper;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class ItsmGatewayClient {
    @Test
    public void testOpenTicket() throws Exception {
        URL url = new URL("http://localhost:7003/itsm/v1/xihe/tickets");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        TicketVO vo = new TicketVO();
        vo.setTicketid("1");
        vo.setProject("ty1");
        //vo.setcTime(new Date());
        String json = JSONHelper.toJSON(vo);
        
        conn.setRequestMethod("POST");
        conn.setRequestProperty("x-dt", "1");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Content-Length", json);
        
        conn.setDoOutput(true);
        long start = System.currentTimeMillis();
        conn.getOutputStream().write(json.getBytes());
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = conn.getInputStream().read(buf)) > 0)
            System.out.println(new String(buf, 0, len));
        
        System.out.println("times:" + (System.currentTimeMillis() - start));
    }
    
    @Test
    public void testAuthenticate() throws Exception {
        URL url = new URL("http://localhost:7003/itsm/v1/xihe/authenticate");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        LoginData login = new LoginData();
        login.setLoginname("ttt");
        login.setPassword("abc");
        
        String json = JSONHelper.toJSON(login);
        
        conn.setRequestMethod("POST");
        conn.setRequestProperty("x-dt", "1");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Content-Length", json);
        
        conn.setDoOutput(true);
        long start = System.currentTimeMillis();
        conn.getOutputStream().write(json.getBytes());
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = conn.getInputStream().read(buf)) > 0)
            System.out.println(new String(buf, 0, len));
        
        System.out.println("times:" + (System.currentTimeMillis() - start));
    }
    
    public void testSyncProjectEngineer() throws Exception {
        URL url = new URL("http://localhost:7003/itsm/v1/xihe/syncdata/projectengineer");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
    }
    
    @Test
    public void testGetEngineer() throws Exception {
        HttpSession session = new HttpSession();
        HttpRequest request = new HttpRequest("http://localhost:7003/itsm/v1/xihe/engineers/mstest15@163.com");
        HttpResponse resp = session.sendGetRequest(request);
        System.out.println(resp.getBody());
        
        RestClientProxy proxy = new RestClientProxy("", "http://localhost:7003/itsm/v1");
        System.out.println(proxy.getService(IItsmEngineer.class).getEngineer("xihe", "mstest15@163.com").readEntity(String.class));
    }
    
    @Test
    public void testUploadAttachment() throws Exception {
        HttpSession session = new HttpSession();
        HttpRequest request = new HttpRequest("http://localhost:7003/itsm/v1/xihe/tickets/9f3f37ae-9427-4108-80c7-21c6d7cb716f/attachments/f90bf063ef744b9ca83dde0b9e2306fb");
        HttpResponse response = session.uploadFile(request, Thread.currentThread().getContextClassLoader().getResource("").getPath() + "u330.jpg");
        
        System.out.println(response.getStatus());
        Assert.assertTrue(response.getStatus() == 200);
    }
    
    @Test
    public void testUpdateTicket() throws Exception {
        HttpSession session = new HttpSession();
        ChangedTicketVO vo = new ChangedTicketVO();
        vo.setTicketid("dbd935e4-3238-400e-a063-1f500da6847c");
        vo.setAction("reject");
        Map<String, String> fields = new HashMap<>();
        fields.put("ticketoprationtype", "1"); // 0 -- 退回， 1 -- 快速完成
        fields.put("appstatus", "6"); // 0 -- 退回， 1 -- 快速完成
        fields.put("backreasonapp", "测试退回");
        vo.setFields(fields);
        
        HttpRequest request = new HttpRequest("http://localhost:7003/itsm/v1/xihe/tickets/dbd935e4-3238-400e-a063-1f500da6847c/confirm");
        request.addExtHeader("Content-type", "application/json;charset-utf-8");
        request.setBody(JSONHelper.toJSON(vo));
        HttpResponse resp = session.sendPutRequest(request);
        System.out.println(resp.getStatus() + ":" + resp.getBody());
    }
    
    
}
